# Copyright (c) 2022 - 2023 by Apex.AI Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@rules_cc//cc:defs.bzl", "cc_library")
load("//bazel:configure_file.bzl", "configure_file")
load("//bazel:configure_version.bzl", "configure_version")

#
# Config & Platform support
#
config_setting(
    name = "linux",
    constraint_values = ["@platforms//os:linux"],
)

config_setting(
    name = "qnx",
    constraint_values = ["@platforms//os:qnx"],
)

config_setting(
    name = "mac",
    constraint_values = ["@platforms//os:osx"],
)

config_setting(
    name = "win",
    constraint_values = ["@platforms//os:windows"],
)

config_setting(
    name = "unix",
    constraint_values = ["@platforms//os:freebsd"],
)

config_setting(
    name = "linux-clang",
    constraint_values = ["@platforms//os:linux"],
    flag_values = {"@bazel_tools//tools/cpp:compiler": "clang"},
)

config_setting(
    name = "linux-gcc",
    constraint_values = ["@platforms//os:linux"],
    flag_values = {"@bazel_tools//tools/cpp:compiler": "gcc"},
)

configure_version(
    name = "iceoryx_versions_h",
    src = "cmake/iceoryx_versions.h.in",
    out = "generated/include/iceoryx_versions.h",
    version_from = "//:VERSION",
)

configure_file(
    name = "platform_settings_hpp",
    src = select({
        ":linux": "linux/cmake/platform_settings.hpp.in",
        ":mac": "mac/cmake/platform_settings.hpp.in",
        ":qnx": "qnx/cmake/platform_settings.hpp.in",
        ":unix": "unix/cmake/platform_settings.hpp.in",
        ":win": "win/cmake/platform_settings.hpp.in",
        "//conditions:default": "linux/cmake/platform_settings.hpp.in",
    }),
    out = "generated/include/iceoryx_platform/platform_settings.hpp",
    config = select({
        ":qnx": {
            "IOX_PLATFORM_LOCK_FILE_PATH_PREFIX": "/var/lock/",
            "IOX_PLATFORM_TEMP_DIR": "/tmp/",
            "IOX_PLATFORM_UDS_SOCKET_PATH_PREFIX": "/tmp/",
        },
        ":win": {
            # use "/" as separator to circumvent the cruelty "C:\\\\\\\\Windows"
            "IOX_PLATFORM_LOCK_FILE_PATH_PREFIX": "C:/Windows/Temp/",
            "IOX_PLATFORM_TEMP_DIR": "C:/Windows/Temp/",
            "IOX_PLATFORM_UDS_SOCKET_PATH_PREFIX": "",
        },
        "//conditions:default": {
            "IOX_PLATFORM_LOCK_FILE_PATH_PREFIX": "/tmp/",
            "IOX_PLATFORM_TEMP_DIR": "/tmp/",
            "IOX_PLATFORM_UDS_SOCKET_PATH_PREFIX": "/tmp/",
        },
    }),
)

#
# Library: iceoryx_platform
#
cc_library(
    name = "iceoryx_platform",
    srcs = select({
        ":linux": glob([
            "linux/source/**",
        ]),
        ":mac": glob([
            "mac/source/**",
        ]),
        ":qnx": glob([
            "qnx/source/**",
        ]),
        ":unix": glob([
            "unix/source/**",
        ]),
        ":win": glob([
            "win/source/**",
        ]),
        "//conditions:default": glob([
            "linux/source/**",
        ]),
    }) + glob(["generic/source/**"]),
    hdrs = select({
        ":linux": glob(["linux/include/**"]),
        ":mac": glob(["mac/include/**"]),
        ":qnx": glob(["qnx/include/**"]),
        ":unix": glob(["unix/include/**"]),
        ":win": glob(["win/include/**"]),
        "//conditions:default": glob(["linux/include/**"]),
    }) + glob(["generic/include/**"]) + [
        ":iceoryx_versions_h",
        ":platform_settings_hpp",
    ],
    includes = select({
        ":linux": ["linux/include"],
        ":mac": ["mac/include"],
        ":qnx": ["qnx/include"],
        ":unix": ["unix/include"],
        ":win": ["win/include"],
        "//conditions:default": ["linux/include"],
    }) + [
        "generated/include",
        "generic/include",
    ],
    linkopts = select({
        ":linux": [
            "-lpthread",
            "-lrt",
        ],
        ":mac": [],
        ":qnx": ["-lsocket"],
        ":unix": [
            "-lpthread",
            "-lrt",
        ],
        ":win": [
            "advapi32.lib",
        ],
        "//conditions:default": [
            "-lpthread",
            "-lrt",
        ],
    }),
    visibility = ["//visibility:public"],
    deps = [],
)
